<?php
    class CategoryAction extends CommonAction {
        public function index(){
            $category=M('category')->order('orders asc')->select();
            $this->category=node_merge($category);
            $this->display();
        }
        //排序
        public function orders(){
            $db=M('category');
            foreach($_POST as $id=>$orders){
                $db->where(array('id'=>$id))->setField('orders',$orders);
            }
            $this->redirect('index');
        }
        //添加
        public function add(){
            $Category=M('category')->select();
            $this->Module=M('module')->select();
            $this->Category=node_merge($Category);
            $this->display();
        }
        public function addHandle(){
            $db=M('category');
            $data=$db->create();
            if(!$data['title']) $this->error('名称不能为空！');
            if(!$data['module']) $this->error('模型不能为空！');
            if(!$data['list']) $this->error('列表页不能为空！');
            if(!$data['content']) $this->error('内容页不能为空！');
            
            /*import('ORG.Net.UploadFile');
            $upload = new UploadFile();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->savePath =  '../Uploads/';// 设置附件上传目录
            $upload->upload();
            $info = $upload->getUploadFileInfo();
            if($info!=null)
            {
                foreach($info as $v){
                    $data['thumb']=$v['savename'];
                }
            }*/
            $db->add($data);
            $this->success('添加成功!');
        }
        //删除
        public function delete(){
            $db=M('category');
            $cout=$db->where(array('pid'=>I('id')))->count();
            if($cout) $this->error('请先删除下级类目！');
            $db->where(array('id'=>I('id')))->delete();
            $this->success('删除成功');
        }
        //修改
        public function modify(){
            $Category=M('category')->select();
            $Category=node_merge($Category);
            $this->Category=$Category;
            $this->mCategory=M('category')->find(I('id'));
            $this->pCategory=M('category')->find($this->mCategory['pid']); 
            $this->mModule=M('module')->find($this->mCategory['module']);
            $this->Module=M('module')->select();
            $this->display();
        }
        public function modifyHandle(){
            $db=M('category');
            $data=$db->create();
            if(!$data['title']) $this->error('名称不能为空！');
            if(!$data['module']) $this->error('模块不能为空！');
            if(!$data['list']) $this->error('列表页不能为空！');
            if(!$data['content']) $this->error('内容页不能为空！');
            
            /*import('ORG.Net.UploadFile');
            $upload = new UploadFile();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->savePath =  '../Uploads/';// 设置附件上传目录
            $upload->upload();
            $info = $upload->getUploadFileInfo();
            if($info!=null)
            {
                foreach($info as $v){
                    $data['thumb']=$v['savename'];
                }
            }*/
            
            $db->where(array('id'=>I('id')))->save($data);
            $this->success('修改成功!');
        }
        //删除图片
        public function deleteImgHandle(){
            
        }
    }
?>